package com.javabasic.algorithm.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiongmin
 * @version 0.0.1
 * @description Created by work on 2021/10/31 8:39 下午
 * @see [500. Keyboard Row](https://leetcode-cn.com/problems/keyboard-row/)
 */
public class KeyboardRow {

    public String[] findWords(String[] words) {
        String[] keyboardRow = new String[3];
        keyboardRow[0] = "qwertyuiop";
        keyboardRow[1] = "asdfghjkl";
        keyboardRow[2] = "zxcvbnm";

        int keyboardLen = 3, tag = 0, wordLen;
        boolean flag = true;
        List<String> result = new ArrayList<>();
        String tempWord;
        for (String word : words) {
            tempWord = word.toLowerCase();
            for (int i = 0; i < keyboardLen; i++) {
                if (keyboardRow[i].contains(tempWord.charAt(0) + "")) {
                    tag = i;
                    break;
                }
            }
            wordLen = word.length();
            flag = true;
            for (int i = 0; i < wordLen; i++) {
                if (!keyboardRow[tag].contains(tempWord.charAt(i) + "")) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                result.add(word);
            }
        }

        String[] resultArr;
        if (result.isEmpty()) {
            resultArr = new String[0];
        } else {
            resultArr = new String[result.size()];
            tag = 0;
            for (String s : result) {
                resultArr[tag++] = s;
            }
        }

        return resultArr;
    }
}
